

package com.hazelcast.internal.util.phonehome;

import com.hazelcast.cache.impl.CacheService;
import com.hazelcast.core.DistributedObject;
import com.hazelcast.instance.impl.Node;

import java.util.Collection;

import static com.hazelcast.internal.util.phonehome.PhoneHomeMetrics.CACHE_COUNT_WITH_WAN_REPLICATION;

/**
 * Provides information about ICache usage
 */
class CacheMetricsProvider implements MetricsProvider {

    @Override
    public void provideMetrics(Node node, MetricsCollectionContext context) {
        Collection<DistributedObject> objects = node.hazelcastInstance.getDistributedObjects();
        long countCacheWithWANReplication = objects.stream().filter(obj -> obj.getServiceName().equals(CacheService.SERVICE_NAME)).map(obj -> node.getConfig().findCacheConfigOrNull(obj.getName())).filter(config -> config != null && config.getWanReplicationRef() != null).count();

        context.collect(CACHE_COUNT_WITH_WAN_REPLICATION, countCacheWithWANReplication);
    }
}
